﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using thinger.Modbuslib;
using thinger.Models;
using Timer = System.Windows.Forms.Timer;

namespace thinger
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            config = new Config() {
                PortName = "COM1",
                BaudRate = 9600,
                DataBits = 8,
                StopBits = System.IO.Ports.StopBits.One,
                Parity = System.IO.Ports.Parity.None,
                SalveId1 = 1,
                SalveId2 = 2,
                SalveId3 = 3,
                SalveId4 = 4,

            };
            try
            {
                 modbus.Open(config.PortName, config.BaudRate, config.Parity, config.DataBits, config.StopBits);
            
            }catch (Exception ex)
            {
                MessageBox.Show("连接失败" + ex.Message);
                    return;

            }
            MessageBox.Show("连接成功");
            cts=new CancellationTokenSource();
            //通信
            Task.Run(new Action(() => {
                Communication();
            }),cts.Token);
            Timer timer = new Timer();
            timer.Interval = 200;
            timer.Tick += Timer_Tick;
            timer.Start();
        }

        private void Timer_Tick(object sender, EventArgs e)
        {
            this.userControl11.Temperature = Temperature1;
            this.userControl11.Sd = Sd1;
            this.userControl12.Temperature = Temperature2;
            this.userControl12.Sd = Sd2;
            this.userControl13.Temperature = Temperature3;
            this.userControl13.Sd = Sd3;
            this.userControl14.Temperature = Temperature4;
            this.userControl14.Sd = Sd4;
        }
        private int Temperature1, Sd1;
        private int Temperature2, Sd2;
        private int Temperature3, Sd3;
        private int Temperature4, Sd4;
        private void Communication()
        {
            while (!cts.IsCancellationRequested)
            {
                ReciveData(config.SalveId1);
                ReciveData(config.SalveId2);
                ReciveData(config.SalveId3);
                ReciveData(config.SalveId4);


            }
        }
        public void ReciveData(byte SlaveId )
        {
                byte[] res = modbus.ReadInputRegs(SlaveId, 1, 2);
                if (res != null && res.Length == 4)
                {    
                      
                    int temp= res[0]+res[1];
                    int humity = res[2] + res[3];
                switch (SlaveId) {
                        case 1: Temperature1 = temp; Sd1 = humity; break;
                        case 2: Temperature2 = temp; Sd2 = humity; break;
                        case 3: Temperature3 = temp; Sd3 = humity; break;
                        case 4: Temperature4 = temp; Sd4 = humity; break; 
                   
                }


          
                }

        }

        //取消线程
        private CancellationTokenSource cts;
        //创建一个通信对象
        private ModbusRTU modbus=new ModbusRTU();
        
        private Config config=new Config();

        private void ParaSet_Click(object sender, EventArgs e)
        {

        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if(cts!=null)
            {
                cts.Cancel();
            }
        }
    }
}
